package org.example.adminservice.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.adminservice.AdminService;
import org.example.mapper.AdminMapper;
import org.example.pojo.admindto.AdminLoginDTO;
import org.example.pojo.admindto.AdminRegisterDTO;
import org.example.pojo.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Transactional
    @Override
    public boolean register(AdminRegisterDTO adminRegisterDTO) {
        log.info(adminRegisterDTO.toString());
        Admin admin1= adminMapper.check(adminRegisterDTO.getAuthorizename());
        if(admin1 == null){
            return false;
        }
        Admin admin = new Admin();
        admin.setId(admin1.getId());
        admin.setAdminname(adminRegisterDTO.getAdminname());
        admin.setPassword(adminRegisterDTO.getPassword());
        admin.setEmail(adminRegisterDTO.getEmail());
        admin.setAuthorizeid(admin1.getId());
        adminMapper.authorize(admin);
        return true;
    }

    @Override
    public Admin login(AdminLoginDTO adminLoginDTO) {
        return adminMapper.login(adminLoginDTO);
    }
}
